package com.xlhj.schedule.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.StrUtil;
import com.xlhj.schedule.entity.ScheduleCron;
import com.xlhj.schedule.service.ScheduleCronService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author: lcj
 * @Date: 2020/11/26 14:07
 * @Description: 定时任务配置类
 * @Version: 0.0.1
 */
@Slf4j
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    @Autowired
    private ApplicationContext context;
    @Autowired
    private ScheduleCronService scheduleCornService;

    @Value("${schedule.switch}")
    private boolean scheduleSwitch;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        if (!scheduleSwitch) {
            return;
        }
        List<ScheduleCron> scheduleCronList = scheduleCornService.list();
        if (scheduleCronList != null && scheduleCronList.size() > 0) {
            for (ScheduleCron scheduleCron : scheduleCronList) {
                if (scheduleCron.getStatus() == 1) {
                    scheduledTaskRegistrar.setScheduler(taskScheduler());
                    scheduledTaskRegistrar.addTriggerTask(new Runnable() {
                        @Override
                        public void run() {
                            log.info("SchedulingConfigurer实现定时任务======>>>" + Thread.currentThread() + LocalDateTime.now().toLocalTime());
                        }
                    }, triggerContext -> {
                        String cronExpression = scheduleCron.getCronExpression();
                        if (StrUtil.isEmpty(cronExpression)) {

                        }
                        return new CronTrigger(cronExpression).nextExecutionTime(triggerContext);
                    });
                }
            }
        }
        /*if (scheduleCronList != null && scheduleCronList.size() > 0) {
            for (ScheduleCron scheduleCron : scheduleCronList) {
                if (scheduleCron.getStatus() == 1) {
                    scheduledTaskRegistrar.addTriggerTask(() -> System.out.println("执行定时任务：" + scheduleCron.getScheduleName() + LocalDateTime.now().toLocalTime()), triggerContext -> {
                        String cronExpression = scheduleCron.getCronExpression();
                        if (StrUtil.isEmpty(cronExpression)) {

                        }
                        return new CronTrigger(cronExpression).nextExecutionTime(triggerContext);
                    });
                }
            }
        }*/
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        return taskScheduler;
    }
}
